layui.use(['form', 'table'], function () {
    let $ = layui.jquery, form = layui.form, table = layui.table;
    //表格渲染
    table.render({
        elem: '#score-list',
        url: '/api/score/get/list',
        toolbar: '#toolbar',
        defaultToolbar: ['filter', 'exports', 'print', {
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: [
            [
                {type: "checkbox", width: 50, fixed: "left"},
                {field: 'stuCode', width: 120, title: '学号', sort: true},
                {field: 'stuName', width: 120, title: '学生姓名'},
                {field: 'className',width: 150, title: '班级名称', sort: true},
                {field: 'examName', width: 200, title: '考试名称'},
                {field: 'score', width: 100, title: '分数'},
                {field: 'createDate', width: 150, title: '创建时间', templet :function (e){
                        return createDateFormat(e.createDate);
                    }},
                {title: '操作', minWidth: 120, templet: '#currentTableBar', fixed: "right", align: "center"}
            ]
        ],
        limits: [10, 15, 20, 25, 50, 100],
        limit: 10,
        page: true
    });

    //搜索
    searchEvent();

    //工具栏
    toolbarEvent()


});

//搜索事件
function searchEvent() {
    layui.form.on('submit(data-search-btn)', function (data) {
        //执行搜索重载
        reload(data)
        return false;
    });
}

//工具栏事件（添加、删除）
function toolbarEvent() {
    layui.table.on('toolbar(currentTableFilter)', function(obj){
        switch(obj.event){
            case 'add':
                addEvent();
                break;
            case 'delete':
                checkboxDelEvent();
                break;
        };
    });
};

//添加事件
function addEvent() {
    let index = layer.open({
        title: '录入学生成绩',
        type: 2,
        shade: 0.2,
        maxmin:true,
        shadeClose: true,
        area: ['80%', '80%'],
        content: '/page/score/edit.html',
    });
    return false;
}

//表格重载
function reload(data) {
    if(!data || data == ''){
        data = layui.table.checkStatus('score-list').data
    }
    layui.table.reload('score-list', {
        page: {
            curr: 1
        },
        where: data.field
    }, 'data');
}